package com.xrtn.controller;

import com.xrtn.bean.InitAdvert;
import com.xrtn.bean.SysUserInfo;
import com.xrtn.service.ISysAdService;
import com.xrtn.service.ISysAuthService;
import com.xrtn.utils.CodeEnum;
import com.xrtn.utils.HttpUtils;
import com.xrtn.utils.ObjUtils;
import com.xrtn.utils.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by TreeJohn on 2018/3/1.
 */

@Api("广告轮播相关接口")
@Controller
@RequestMapping("/ad")
@CrossOrigin(origins = "*", maxAge = 3600)
public class AdController {

    @Autowired
    private ISysAdService adService;



    /**
     * 进入预约管理页面
     * @return
     */
    @RequestMapping(value = "/gotoInit",method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView goTobespeak(ModelAndView modelAndView){
        modelAndView.setViewName("basicSetting");
        return modelAndView;
    }

    /**
     * 创建广告
     * @param ad
     * @return
     */
    @ApiOperation(value = "创建广告",notes="创建广告")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="adName",dataType="string",required=true,value="轮播title",defaultValue = "GYT000001"),
            @ApiImplicitParam(paramType="query",name="adImage",dataType="string",required=true,value="广告图片URL",defaultValue = "https://t1.onvshen.com:85/gallery/17596/24921/0.jpg"),
            @ApiImplicitParam(paramType="query",name="adIndex",dataType="int",required=true,value="自定义排序index",defaultValue = "0"),
            @ApiImplicitParam(paramType="query",name="skipUrl",dataType="string",required=true,value="跳转链接",defaultValue = "https://www.baidu.com"),
            @ApiImplicitParam(paramType="query",name="adStatus",dataType="int",required=true,value="当前状态:0-启用 1-下线",defaultValue = "0"),
            @ApiImplicitParam(paramType="query",name="adType",dataType="int",required=true,value="类型：0轮播图，1广播",defaultValue = "0"),
            @ApiImplicitParam(paramType="query",name="subclinicId",dataType="int",required=true,value="分店ID（当为0时为总店）",defaultValue = "0"),
            @ApiImplicitParam(paramType="query",name="createUserId",dataType="int",required=true,value="当前创建人",defaultValue = "1")
    })
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData createAd(HttpServletRequest request,InitAdvert ad){

        //SysUserInfo userInfo = (SysUserInfo) request.getSession().getAttribute(HttpUtils.CURRENT_USER_4_POJO);

        //ad.setSubclinicId(userInfo.getSubclinicId());

        ResponseData data = new ResponseData();

        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{ad.getAdName(), ad.getAdImage(),ad.getAdIndex(),
                ad.getSkipUrl(),ad.getAdStatus(),ad.getAdType(), ad.getSubclinicId(), ad.getCreateUserId()});

        if(ObjUtils.isNotEmpty(codeEnum)){
            data.setCodeEnum(codeEnum);
            return data;
        }

        ad.setCreateTime(new Date());

        int count = adService.insertAd(ad);
        if(count < 0 ){
            data.setCodeEnum(CodeEnum.CODE_21001);
        }

        Map<String,Object> retMap = new HashMap<>();
        retMap.put("adId", count);
        data.setResults(retMap);

        // 返回数据
        return data;
    }

    /**
     * 修改广告
     * @param ad
     * @return
     */
    @ApiOperation(value = "修改广告",notes="修改广告")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="adName",dataType="string",required=false,value="轮播title",defaultValue = "GYT000001"),
            @ApiImplicitParam(paramType="query",name="adImage",dataType="string",required=false,value="广告图片URL",defaultValue = "https://t1.onvshen.com:85/gallery/17596/24921/0.jpg"),
            @ApiImplicitParam(paramType="query",name="adIndex",dataType="int",required=false,value="自定义排序index",defaultValue = "0"),
            @ApiImplicitParam(paramType="query",name="skipUrl",dataType="string",required=false,value="跳转链接",defaultValue = "https://www.baidu.com"),
            @ApiImplicitParam(paramType="query",name="adStatus",dataType="int",required=false,value="当前状态:0-启用 1-下线",defaultValue = "0"),
            @ApiImplicitParam(paramType="query",name="adType",dataType="int",required=false,value="类型：0轮播图，1广播",defaultValue = "0"),
            @ApiImplicitParam(paramType="query",name="subclinicId",dataType="int",required=false,value="分店ID（当为0时为总店）",defaultValue = "0"),
            @ApiImplicitParam(paramType="query",name="createUserId",dataType="int",required=false,value="当前创建人",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="adId",dataType="int",required=false,value="要修改广告id",defaultValue = "4")
    })
    @RequestMapping(value = "/modify",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData modifyAd(InitAdvert ad){

        ResponseData data = new ResponseData();

        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{ad.getAdId()});

        if(ObjUtils.isNotEmpty(codeEnum)){
            data.setCodeEnum(codeEnum);
            return data;
        }

        int count = adService.updateAd(ad);
        if(count < 0 ){
            data.setCodeEnum(CodeEnum.CODE_21002);
        }

        Map<String,Object> retMap = new HashMap<>();
        data.setResults(retMap);

        // 返回数据
        return data;
    }

    /**
     * 查询店内广告
     * @return
     */
    @ApiOperation(value = "查询店内广告",notes="查询店内广告")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="subclinicId",dataType="int",required=true,value="分店ID（当为0时为总店）",defaultValue = "0")
    })
    @RequestMapping(value = "/getAdBySubclinicId",method = RequestMethod.GET)
    @ResponseBody
    public ResponseData getAdBySubclinicId(HttpServletRequest request,Integer subclinicId){

        //SysUserInfo userInfo = (SysUserInfo) request.getSession().getAttribute(HttpUtils.CURRENT_USER_4_POJO);

        ResponseData data = new ResponseData();

        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{subclinicId});

        if(ObjUtils.isNotEmpty(codeEnum)){
            data.setCodeEnum(codeEnum);
            return data;
        }

        data.setResults(adService.selectAdBySubclinicId(subclinicId));

        // 返回数据
        return data;
    }

}
